//
//  HARegisterVC.m
//  HA
//
//  Created by sy on 2023/12/17.
//

#import "HARegisterVC.h"
#import "HAWebVC.h"
#import "HATabBarViewController.h"
#import "AppDelegate.h"

@interface HARegisterVC ()
/***/
@property (nonatomic, strong) UITextField *numTextField;
/***/
@property (nonatomic, strong) UITextField *phoneTextField;
/***/
@property (nonatomic, strong) UITextField *codeTextField;
/***/
@property (nonatomic, strong) UITextField *pwdTextField;
/***/
@property (nonatomic, strong) UIButton *codeButton;


@end

@implementation HARegisterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(countDownNotification) name:@"OYCountDownNotification" object:nil];
    [kCountDownManager addSourceWithIdentifier:@"YHRegisterDownNotification"];
    
    // Do any additional setup after loading the view.
}

-(void)dealloc {
    [kCountDownManager removeSourceWithIdentifier:@"YHRegisterDownNotification"];
}

- (void)countDownNotification {
    NSInteger timeInterval = [kCountDownManager timeIntervalWithIdentifier:@"YHRegisterDownNotification"];
    
    if (61-timeInterval > 0) {
        [self.codeButton setTitle:[NSString stringWithFormat:@"%ld",(long)(61-timeInterval)] forState:UIControlStateNormal];
        self.codeButton.enabled = NO;
    }else {
        [self.codeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        self.codeButton.enabled = YES;
    }
}


-(void)toLoginButtonClick {
    
    [self.view endEditing:YES];
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)codeButtonClick {
    
    [self.view endEditing:YES];
    
    if ([NSString isEmptyString:self.phoneTextField.text]) {
        [MBProgressHUD showError:@"请输入手机号"];
        return;
    }
    
    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodGET;
    request.requestUrl = @"sms/send";
    request.parms = @{
        @"event":@"register",
        @"mobile":self.phoneTextField.text?:@"",
    };
    [MBProgressHUD showLoading:self.view];

    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {

            [MBProgressHUD hideHUDForView:self.view];

            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                [MBProgressHUD showSuccess:dataModel.msg];
                [kCountDownManager reloadSourceWithIdentifier:@"YHRegisterDownNotification"];
                [kCountDownManager start];
            }else{
                [MBProgressHUD showError:dataModel.msg];
            }
        }
    }];
}

-(void)registerButtonClick {
    
    [self.view endEditing:YES];
    
    if ([NSString isEmptyString:self.phoneTextField.text]) {
        [MBProgressHUD showError:@"请输入手机号"];
        return;
    }
    
    if ([NSString isEmptyString:self.codeTextField.text]) {
        [MBProgressHUD showError:@"请输入验证码"];
        return;
    }
    
    if ([NSString isEmptyString:self.pwdTextField.text]) {
        [MBProgressHUD showError:@"请输入密码"];
        return;
    }

    if ([NSString isEmptyString:self.numTextField.text]) {
        [MBProgressHUD showError:@"请输入推荐人工号"];
        return;
    }
    
    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodGET;
    request.requestUrl = @"auth/register";
    request.parms = @{
        @"mobile":self.phoneTextField.text?:@"",
        @"verifi_code":self.codeTextField.text?:@"",
        @"password":self.pwdTextField.text?:@"",
        @"agent_id":self.numTextField.text?:@"",
        @"device":@"ios",
    };
    [MBProgressHUD showLoading:self.view];

    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {

            [MBProgressHUD hideHUDForView:self.view];

            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                HAUserInfoModel *userInfoModel = [HAUserInfoModel mj_objectWithKeyValues:[dataModel.data valueForKey:@"userinfo"]];
                [HAHelper keepUserInfo:userInfoModel];
                
                [MBProgressHUD showSuccess:@"登录成功"];
                [AppDelegate single].window.rootViewController = [[HATabBarViewController alloc]init];
            }else{
                [MBProgressHUD showError:dataModel.msg];
            }
        }
    }];

}

-(void)protocolClick {
    HAWebVC *vc = [[HAWebVC alloc]init];
    vc.urlStr = [NSString stringWithFormat:@"%@policy/policy.html", kHaBaseUrl];
    vc.tit = @"用户协议";
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)setupUI {
    
    self.baseTLabel.text = @"在线注册";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titLabel = [UILabel ha_NLabelWithFont:20 textColor:@"#000000" text:@"注册" alignment:0];
    [self.view addSubview:titLabel];
    [titLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.top.mas_equalTo(app_topH+44+40);
    }];
    
    UIView *line1 = [UIView ha_createView];
    line1.backgroundColor = [UIColor colorWithHexString:@"#E3E3E3"];
    [self.view addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.right.mas_equalTo(-40);
        make.top.mas_equalTo(titLabel.mas_bottom).offset(120);
        make.height.equalTo(@0.8);
    }];
    
    UIView *line2 = [UIView ha_createView];
    line2.backgroundColor = [UIColor colorWithHexString:@"#E3E3E3"];
    [self.view addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.right.mas_equalTo(-40);
        make.top.mas_equalTo(line1.mas_bottom).offset(55);
        make.height.equalTo(@0.8);
    }];
    
    UIView *line3 = [UIView ha_createView];
    line3.backgroundColor = [UIColor colorWithHexString:@"#E3E3E3"];
    [self.view addSubview:line3];
    [line3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.right.mas_equalTo(-40);
        make.top.mas_equalTo(line2.mas_bottom).offset(55);
        make.height.equalTo(@0.8);
    }];

    UIView *line4 = [UIView ha_createView];
    line4.backgroundColor = [UIColor colorWithHexString:@"#E3E3E3"];
    [self.view addSubview:line4];
    [line4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.right.mas_equalTo(-40);
        make.top.mas_equalTo(line3.mas_bottom).offset(55);
        make.height.equalTo(@0.8);
    }];
    
    UIImageView *numImgView = [UIImageView ha_createImageView];
    numImgView.image = [UIImage imageNamed:@"ha_42"];
    [self.view addSubview:numImgView];
    [numImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.bottom.mas_equalTo(line1.mas_top).offset(-12);
        make.width.mas_offset(numImgView.image.size.width);
        make.height.mas_offset(numImgView.image.size.height);
    }];

    _numTextField = [[UITextField alloc]init];
    _numTextField.textColor = [UIColor colorWithHexString:@"#333333"];
    _numTextField.font = [UIFont systemFontOfSize:14];
    _numTextField.placeholder = @"邀请人工号";
    _numTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
//    [_phoneTextField addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:_numTextField];
    [_numTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(numImgView);
        make.left.equalTo(numImgView.mas_right).offset(10);
        make.right.equalTo(line1);
        make.height.equalTo(@28);
    }];


    UIImageView *phoneImgView = [UIImageView ha_createImageView];
    phoneImgView.image = [UIImage imageNamed:@"ha_41"];
    [self.view addSubview:phoneImgView];
    [phoneImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.bottom.mas_equalTo(line2.mas_top).offset(-12);
        make.width.mas_offset(phoneImgView.image.size.width);
        make.height.mas_offset(phoneImgView.image.size.height);
    }];

    _phoneTextField = [[UITextField alloc]init];
    _phoneTextField.textColor = [UIColor colorWithHexString:@"#333333"];
    _phoneTextField.font = [UIFont systemFontOfSize:14];
    _phoneTextField.placeholder = @"请输入手机号";
    _phoneTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
//    [_phoneTextField addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:_phoneTextField];
    [self.phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(phoneImgView);
        make.left.equalTo(phoneImgView.mas_right).offset(10);
        make.right.equalTo(line2);
        make.height.equalTo(@28);
    }];
    
    UIImageView *codeImgView = [UIImageView ha_createImageView];
    codeImgView.image = [UIImage imageNamed:@"ha_43"];
    [self.view addSubview:codeImgView];
    [codeImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.bottom.mas_equalTo(line3.mas_top).offset(-12);
        make.width.mas_offset(codeImgView.image.size.width);
        make.height.mas_offset(codeImgView.image.size.height);
    }];
    
    _codeButton = [[UIButton alloc]init];
    [_codeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [_codeButton setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
    _codeButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [_codeButton addTarget:self action:@selector(codeButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_codeButton];
    [_codeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(codeImgView);
        make.right.equalTo(line3);
        make.height.equalTo(@30);
        make.width.equalTo(@84);

    }];
    
    _codeTextField = [[UITextField alloc]init];
    _codeTextField.textColor = [UIColor colorWithHexString:@"#333333"];
    _codeTextField.font = [UIFont systemFontOfSize:13];
    _codeTextField.placeholder = @"请输入验证码";
    _codeTextField.secureTextEntry = YES;
    _codeTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _codeTextField.keyboardType = UIKeyboardTypeNumberPad;
//    [_pwdTextField addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:_codeTextField];
    [_codeTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(codeImgView);
        make.left.equalTo(codeImgView.mas_right).offset(10);
        make.right.equalTo(_codeButton.mas_left);
        make.height.equalTo(@28);
    }];


    UIImageView *pwdImgView = [UIImageView ha_createImageView];
    pwdImgView.image = [UIImage imageNamed:@"ha_44"];
    [self.view addSubview:pwdImgView];
    [pwdImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.bottom.mas_equalTo(line4.mas_top).offset(-12);
        make.width.mas_offset(pwdImgView.image.size.width);
        make.height.mas_offset(pwdImgView.image.size.height);
    }];

    _pwdTextField = [[UITextField alloc]init];
    _pwdTextField.textColor = [UIColor colorWithHexString:@"#333333"];
    _pwdTextField.font = [UIFont systemFontOfSize:13];
    _pwdTextField.placeholder = @"请输入密码";
    _pwdTextField.secureTextEntry = YES;
    _pwdTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
//    [_pwdTextField addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:_pwdTextField];
    [self.pwdTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(pwdImgView);
        make.left.equalTo(pwdImgView.mas_right).offset(10);
        make.right.equalTo(line4);
        make.height.equalTo(@28);
    }];

    UIButton *toLoginButton = [[UIButton alloc]init];
    [toLoginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [toLoginButton setTitle:@"已有账号？去登录" forState:UIControlStateNormal];
    toLoginButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [toLoginButton setTitleColor:[UIColor colorWithHexString:@"#9E312E"] forState:UIControlStateNormal];
    [toLoginButton addTarget:self action:@selector(toLoginButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:toLoginButton];
    [toLoginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@102);
        make.height.equalTo(@28);
        make.top.equalTo(line4.mas_bottom).offset(18);
        make.right.equalTo(line4);
    }];


    UIButton *registerButton = [[UIButton alloc]init];
    [registerButton setTitle:@"在线开通" forState:UIControlStateNormal];
    [registerButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    registerButton.titleLabel.font = [UIFont systemFontOfSize:16];
    registerButton.backgroundColor = [UIColor colorWithHexString:@"#9E312E"];
    registerButton.layer.cornerRadius = 4;
    registerButton.layer.masksToBounds = true;
    [registerButton addTarget:self action:@selector(registerButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registerButton];
    [registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(42);
        make.right.mas_equalTo(-42);
        make.top.mas_equalTo(line4.mas_bottom).offset(108);
        make.height.mas_offset(44);
    }];
    
    //
    WS(weakSelf)
    NSString *agreementText = @"开通则代表您同意《用户协议》";
    NSMutableAttributedString *agreementAttr = [[NSMutableAttributedString alloc] initWithString:agreementText];
    NSRange range1 = [[agreementAttr string]rangeOfString:@"《用户协议》"];
    [agreementAttr setTextHighlightRange:range1 color:[UIColor colorWithHexString:@"#2E63B1"] backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        [weakSelf protocolClick];
    }];
    YYLabel *agreementLabel = [[YYLabel alloc]init];
    agreementLabel.textColor = [UIColor colorWithHexString:@"#111a34"];
    agreementLabel.textAlignment = 0;
    agreementLabel.numberOfLines = 0;
    agreementLabel.font = [UIFont systemFontOfSize:13];
    agreementLabel.attributedText = agreementAttr;
    [self.view addSubview:agreementLabel];
    [agreementLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-app_bottomH-12);
        make.centerX.mas_equalTo(0);
    }];
}
@end
